package tcpFirmware.controller;

import tcpFirmware.ResponseBody;
import tcpFirmware.Routes;
import tcpFirmware.Type;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.Socket;
import java.util.HashMap;

public class DownloadController {
    @Routes(route = "download/image")
    public void download(Socket socket, HashMap<String,String> head, byte[] bytes){
        int after = Integer.parseInt(head.get("after"));
        try {
            FileInputStream inputStream = new FileInputStream(head.get("filepath"));
            long skip = inputStream.skip(after);
            byte[] bytes1 = new byte[20480];
            int len=inputStream.read(bytes1);
            ResponseBody responseBody = new ResponseBody(socket);
            responseBody.setType(Type.img.type);
            if(len!=20480){
                responseBody.setHeader("finish","true");
            }else {
                responseBody.setHeader("finish","false");
            }
            responseBody.sendResponse(bytes1,head.get("filename"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
